//
//  RootTabBarViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "DefaultViewController.h"
#import "FindViewController.h"
#import "MeViewController.h"
#import "MessageViewController.h"
#import "JHZTabBar.h"
#import "AddViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "HJZUploadViewController.h"
#import <PopMenu.h>

@interface RootTabBarViewController ()<JHZTabBarDelegate>
@property (nonatomic, strong) PopMenu *popMenu;
@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DefaultViewController *defaultVC = [[DefaultViewController alloc] init];
    FindViewController *findVC = [[FindViewController alloc] init];
    MessageViewController *messageVC = [[MessageViewController alloc] init];
    MeViewController *meVC = [[MeViewController alloc] init];
    // 调用自定义方法 设置根视图控制器
    self.viewControllers = [self setupNavgationController:@[defaultVC, messageVC, findVC, meVC]];
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    if (accessToken) {
        self.selectedIndex = 0;
    }else {
        self.selectedIndex = 3;
    }
    
    JHZTabBar *tabBar = [[JHZTabBar alloc] init];
    // 设置代理
    tabBar.delegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    // 修改tabBar为自定义tabBar
    [self setValue:tabBar forKey:@"tabBar"];
    
    
}


- (void)tabBarDidClickPlusButton:(JHZTabBar *)tabBar
{
//    AddViewController *addVC = [[AddViewController alloc]init];
//    __block RootTabBarViewController *blockSelf = self;
//    addVC.block = ^{
//        HJZUploadViewController *uploadVC = [[HJZUploadViewController alloc] init];
//        
//        uploadVC.uploadType = UploadTypeStatus;
//        UINavigationController *createCommentNav = [[UINavigationController alloc] initWithRootViewController:uploadVC];
//        
//        [blockSelf presentViewController:createCommentNav animated:YES completion:nil];
//    
//    };
    [self showMenu];
    // 1. 获取URL
    CFURLRef urlRef = (__bridge CFURLRef)([[NSBundle mainBundle] URLForResource:@"music.caf" withExtension:nil]);
    // 2. 创建保存soundID 的变量
    SystemSoundID soundID;
    // 3. 通过url, 和soundID的地址, 接收对应的音效soundID
    AudioServicesCreateSystemSoundID(urlRef, &soundID);
    AudioServicesPlaySystemSound(soundID);
   // [self presentViewController:addVC animated:YES completion:nil];
}

// 循环创建4个页面
- (NSMutableArray *)setupNavgationController:(NSArray *)viewControllers {
    NSMutableArray *navArray = [NSMutableArray array];
    NSArray *imageNameArray = @[@"home", @"message", @"find", @"me"];
    NSArray *titleArray = @[@"首页", @"消息", @"发现", @"我"];
    for (int i = 0; i < 4; i++) {
        // 导航控制器
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllers[i]];
        
        NSString *imageName = [NSString stringWithFormat:@"%@.png", imageNameArray[i]];
        UIImage *image = [UIImage imageNamed:imageName];
        //image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
//        NSString *selectImageName = [NSString stringWithFormat:@"%@%d.png", imageNameArray[i], 1];
//        UIImage *selectedImage =[UIImage imageNamed:selectImageName];
//        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        // 初始化一个tabBarItem
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[i] image:image tag:100];
        
        [navArray addObject:nav];
        
    }
    return navArray;
}


- (void)showMenu {
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
    
    MenuItem *menuItem = [MenuItem itemWithTitle:@"weizi" iconName:@"wenzi"];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"zhaopian" iconName:@"zhaopian" glowColor:[UIColor colorWithRed:0.840 green:0.264 blue:0.208 alpha:0.800]];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"toutiao" iconName:@"toutiao" glowColor:[UIColor colorWithRed:0.232 green:0.442 blue:0.687 alpha:0.800]];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"ahha" iconName:@"wenzi" glowColor:[UIColor colorWithRed:0.000 green:0.509 blue:0.687 alpha:0.800]];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"hha" iconName:@"wenzi" glowColor:[UIColor colorWithRed:0.687 green:0.164 blue:0.246 alpha:0.800]];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"hahah" iconName:@"wenzi" glowColor:[UIColor colorWithRed:0.258 green:0.245 blue:0.687 alpha:0.800]];
    [items addObject:menuItem];
    
    if (!_popMenu) {
        _popMenu = [[PopMenu alloc] initWithFrame:self.view.bounds items:items];
        _popMenu.menuAnimationType = kPopMenuAnimationTypeSina;
    }
    if (_popMenu.isShowed) {
        return;
    }
    __block RootTabBarViewController *blockSelf = self;
    _popMenu.didSelectedItemCompletion = ^(MenuItem *selectedItem) {
        HJZUploadViewController *uploadVC = [[HJZUploadViewController alloc] init];
        uploadVC.uploadType = UploadTypeStatus;
        UINavigationController *createCommentNav = [[UINavigationController alloc] initWithRootViewController:uploadVC];

        [blockSelf presentViewController:createCommentNav animated:YES completion:nil];

    };
    
    [_popMenu showMenuAtView:self.view];
    
    //    [_popMenu showMenuAtView:self.view startPoint:CGPointMake(CGRectGetWidth(self.view.bounds) - 60, CGRectGetHeight(self.view.bounds)) endPoint:CGPointMake(60, CGRectGetHeight(self.view.bounds))];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
